// list-openid-servers.js

var ListOpenIDServers = {};

ListOpenIDServers.load = function() {
    var name = window.arguments[0].name;
    var exp = document.getElementById("list-open-id-servers-description");
    exp.value = exp.value + " " + name;
    ListOpenIDServers.refresh();
};

ListOpenIDServers.unload = function() {
    // do nothing
};

ListOpenIDServers.refresh = function() {
    var rowid = window.arguments[0].id
    var list = document.getElementById("openid-servers");
    for (var i = list.getRowCount(); i > 0; i--) {
        list.removeItemAt(i-1);
    }
    var servers = AppalachianDB.listServers(rowid);
    for (var i = 0; i < servers.length; i++) {
        var server = servers[i];
        var item = document.createElement("listitem");
        item.setAttribute("value", server["id"]);

        var listcell;

        listcell = document.createElement("listcell");
        listcell.setAttribute("label", server["server"]);
        item.appendChild(listcell);
        list.appendChild(item);
    }
    ListOpenIDServers.unselect();
};

ListOpenIDServers.current = function() {
    var list = document.getElementById("openid-servers");
    var item = list.selectedItem;
    return item.value;
};

ListOpenIDServers.modify = function() {
    var id = ListOpenIDServers.current();
    window.openDialog("chrome://appalachian/content/modify-openid-server.xul", "appalachian-modify-openid-server", "chrome,modal", { id : id, openid : window.arguments[0].id } );
    ListOpenIDServers.refresh();
};

ListOpenIDServers.add = function() {
    window.openDialog("chrome://appalachian/content/add-openid-server.xul", "appalachian-add-openid-server", "chrome,modal", { openid : window.arguments[0].id });
    ListOpenIDServers.refresh();
};

ListOpenIDServers.delete = function() {
    var id = ListOpenIDServers.current();
    var count = AppalachianDB.countServers(window.arguments[0].id);
    var bundle = document.getElementById("list-openid-servers-bundle");
    if (count > 1) {
        var confmsg = bundle.getString("delconfirm");
        if (confirm(confmsg)) {
            AppalachianDB.deleteServer(id);
            ListOpenIDServers.refresh();
            ListOpenIDServers.unselect();
        }
    } else {
        var countmsg = bundle.getString("delcount");
        alert(countmsg);
    }
};

ListOpenIDServers.selected = function() {
    document.getElementById("list-openid-servers-modify-button").disabled = false;
    document.getElementById("list-openid-servers-delete-button").disabled = false;
}

ListOpenIDServers.unselect = function() {
    document.getElementById("list-openid-servers-modify-button").disabled = true;
    document.getElementById("list-openid-servers-delete-button").disabled = true;
}

ListOpenIDServers.close = function() {
    window.close();
};
